package name.suray.andrey;

import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.Map;

import org.apache.commons.lang.RandomStringUtils;

public class Top {
	public static void main(String[] args){
		System.out.println("Top main...");
		ParseUnit pu = new ParseUnit();
		Map<String, String> map = null;
		
		try {
			
			map = pu.parse();					//FileNotFoundException
			MyCompiler comp = new MyCompiler();
			String className = map.get("class");			
			Class<?> c = comp.compile(className);
			Object o = c.newInstance();
			
			System.out.println();
			System.out.println("Fields before reflection-init:");
			for(Field f: c.getDeclaredFields()) {
				System.out.print(f.getName() + " = ");
				f.setAccessible(true);
				System.out.print(f.get(o));
				System.out.println();
				
				String rand = RandomStringUtils.random(5,true,true);
				f.set(o, rand);					//set random values to fields
			}
			
			System.out.println();
			System.out.println("Fields after reflection-init:");
			for(Field f: c.getDeclaredFields()) {
				
				System.out.print(f.getName() + " = ");
				f.setAccessible(true);
				System.out.print(f.get(o));
				System.out.println();
				
			}
			
			Fields myObj = (Fields) o;
			System.out.println();
			System.out.println("Fields before self-init:");
			myObj.print();
			myObj.init();
			System.out.println();
			System.out.println("Fields after self-init:");
			myObj.print();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}//end of main
}//end of class Top
